Skip to main content

Python File Handling Basics

When working with computer applications, data often needs to be stored and retrieved from a persistent medium like a disk file. Python provides built-in functions and methods to handle file input and output operations efficiently.

Input and Output Streams​

Python uses standard input/output streams (stdin and stdout) to interact with input and output devices. The input() function reads input from the standard input stream (usually the keyboard), while the print() function sends output to the standard output stream (typically the display monitor).

name = input("Enter your name: ")
print("Hello,", name)

The open() Function​

The open() function in Python is used to create a file object, which is then used to perform various file operations. It takes parameters such as the file name, access mode, and buffering options.

Syntax​

file_object = open(file_name [, access_mode] [, buffering])

Here are the parameter details

- `file_name`: The name of the file to be accessed.
- `access_mode`: The mode in which the file should be opened (e.g., read, write, append).
- `buffering`: Optional parameter for buffering options.

File Opening Modes​

Python supports various file opening modes, each serving a different purpose:

ModeDescription
rRead mode (default).
rbRead mode in binary format.
r+Read and write mode.
rb+Read and write mode in binary format.
wWrite mode (creates a new file or overwrites existing content).
wbWrite mode in binary format.
w+Write and read mode.
wb+Write and read mode in binary format.
aAppend mode (writes data at the end of the file).
abAppend mode in binary format.
a+Append and read mode.
ab+Append and read mode in binary format.
xExclusive creation mode (fails if the file already exists).

Example​

# Open a file
file_obj = open("example.txt", "w")
file_obj.write("Hello, World!")
file_obj.close()

In this example, we create a new file named "example.txt" in write mode, write the string "Hello, World!" to it, and then close the file.